package trade;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;

import net.ck.hzq.R;

import java.util.List;

import base.BaseActivity;
import dao.TradeRecordItem;
import trade.presenter.TradeRecordPresenter;
import trade.view.TradeRecordView;

/**
 * by BF00233 on 2018/12/21.
 */
public class TradeRecordActivity extends BaseActivity implements TradeRecordView {

    private TradeRecordPresenter mPresenter;

    private DelegateAdapter mDelegateAdapter;

    RecyclerView recyclerView;


    public static void start(Context context) {
        Intent intent = new Intent(context, TradeRecordActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_trade_record;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
        mDelegateAdapter = new DelegateAdapter(layoutManager);
        recyclerView = findViewById(R.id.recycler_view);
//        refreshLayout = findViewById(R.id.twinklingRefreshLayout);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mDelegateAdapter);
        mPresenter = new TradeRecordPresenter(this);
        mPresenter.getTradeRecord();
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
//        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
//            @Override
//            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
//                mPresenter.getTradeRecord();
//            }
//
//            @Override
//            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
//                mPresenter.getTradeRecord();
//            }
//        });
    }

    @Override
    public void showTradeRecord(@NonNull List<TradeRecordItem> items) {
        if (mDelegateAdapter == null) {
            return;
        }
//        else {
//            if (recyclerView.getAdapter() != null) {
//                recyclerView.removeAllViews();
//            }
//        }
        for (TradeRecordItem item : items) {
            TradeMonthAdapter monthAdapter = new TradeMonthAdapter(this, item.getMonth());
            mDelegateAdapter.addAdapter(monthAdapter);
            TradeItemAdapter itemAdapter = new TradeItemAdapter(this, item.getMonth());
            mDelegateAdapter.addAdapter(itemAdapter);
            itemAdapter.setupData(item.getMerchants());
        }
    }

    @Override
    public BaseActivity getHostActivity() {
        return this;
    }

    @Override
    public void showProgress() {
        showProgressDialog();
    }

    @Override
    public void hideProgress() {
        dismissProgressDialog();
    }
}
